layui.use(['table','form'], function () {
    var table = layui.table;
    var form = layui.form;
    //-------分类管理表格s--------
    var sort = table.render({
        elem: '#sort-table'
        , height: 'full-180'
        , url: '/admin/sort/ajax'
        , page: true
        , cols: [[
            {field: 'sortid', title: 'ID'}
            , {field: 'sortname', title: '分类名'}
            , {field: '', title: '操作', toolbar: '#barDemo'}
        ]]
    });
    table.on('tool(sort-table-filter)', function (obj) {
        var data = obj.data;
        if (obj.event == 'del') {
            layer.confirm('是否要删除' + data.sortname + '这个分类', function (index) {
                $.ajax({
                    type: 'post'
                    , url: '/admin/sort/dell'
                    , dataType: 'json'
                    , data: {sortid: data.sortid}
                    , success: function (obj1) {
                        if (obj1.code == 1) {
                            obj.del();
                        }
                        layer.alert(obj1.msg);
                    }
                });
                layer.close(index);
            });
        } else if (obj.event == 'edit') {
            layer.prompt({
                title: '修改分类名'
                , formType: 0
                , value: data.sortname
            }, function (value, index, elem) {
                $.ajax({
                    type: 'post'
                    , url: '/admin/sort/editsort'
                    , dataType: 'json'
                    , data: {sortid: data.sortid, sortname: value}
                    , success: function (obj) {
                        console.log(obj);
                        if (obj.code === 1) {
                            //重载
                            sort.reload('sort-table');
                        }
                        layer.alert(obj.msg);
                        layer.close(index);
                    }
                });
            });
        }
    });
    var $ = layui.$, active = {
        addSort: function () { //获取选中数据
            layer.prompt({
                title: '添加新分类'
                , formType: 0
            }, function (value, index, elem) {
                $.ajax({
                    type: 'post'
                    , url: '/admin/sort/addsort'
                    , dataType: 'json'
                    , data: {sortname: value}
                    , success: function (obj) {
                        console.log(obj);
                        if (obj.code === 1) {
                            //重载
                            sort.reload('sort-table');
                        }
                        layer.alert(obj.msg);
                        layer.close(index);
                    }
                });
            });
        }
    };
    $('.demoTable .layui-btn').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
    //-------分类管理表格e--------


    //-------会员管理表格s--------
    var user = table.render({
        elem: '#user-table'
        , height: 'full-180'
        , url: '/admin/usermanger/ajax'
        , page: true
        , cols: [[
            {field: 'userid', title: 'ID'}
            , {field: 'username', title: '用户名'}
            , {field: 'usernick', title: '用户昵称'}
            , {field: 'userstat', title: '用户权限'}
            , {field: '', title: '操作', toolbar: '#userBar'}
        ]]
    });
    table.on('tool(user-table-filter)', function (obj) {
        var data = obj.data;
        if (obj.event == 'del') {
            if (obj.data.userid === 1) {
                layer.alert('管理员不允许删除');
                return false;
            }
            layer.confirm('是否要删除' + data.username + '这个用户', function (index) {
                $.ajax({
                    type: 'post'
                    , url: '/admin/usermanger/dell'
                    , dataType: 'json'
                    , data: {userid: data.userid}
                    , success: function (obj1) {
                        if (obj1.code == 1) {
                            obj.del();
                        }
                        layer.alert(obj1.msg);
                    }
                });
                layer.close(index);
            });
        } else if (obj.event == 'edit') {
            layer.open({
                type: 2 //Page层类型
                , area: ['550px', '400px']
                , title: '修改会员'
                , shade: 0.6 //遮罩透明度
                , maxmin: true //允许全屏最小化
                , anim: 1 //0-6的动画形式，-1不开启
                , content: '/admin/usermanger/edit?userid=' + data.userid
                , end: function () {
                    user.reload('user-table');

                }
            });
        }
    });
    var $ = layui.$, active = {
        addSort: function () { //获取选中数据
            layer.open({
                type: 2 //Page层类型
                , area: ['550px', '400px']
                , title: '添加会员'
                , shade: 0.6 //遮罩透明度
                , maxmin: true //允许全屏最小化
                , anim: 1 //0-6的动画形式，-1不开启
                , content: '/admin/usermanger/adduser'
                , end: function () {
                    user.reload('user-table');

                }
            });
        }
    };
    $('.userTable .layui-btn').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
    //-------会员管理表格e--------



    //-------留言管理表格s--------
    var message = table.render({
        elem: '#message-table'
        , height: 'full-180'
        , url: '/admin/message/ajax'
        , page: true
        , cols: [[
            {field: 'messageid', title: 'ID'}
            , {field: 'userid', title: '用户账号'}
            , {field: 'content', title: '留言内容'}
            , {field: '', title: '操作', toolbar: '#messageBar'}
        ]]
    });
    table.on('tool(message-table-filter)', function (obj) {
        var data = obj.data;
        if (obj.event == 'del') {
            layer.confirm('是否要删除' + data.userid + '的这条评论', function (index) {
                $.ajax({
                    type: 'post'
                    , url: '/admin/message/dell'
                    , dataType: 'json'
                    , data: {messageid: data.messageid}
                    , success: function (obj1) {
                        if (obj1.code == 1) {
                            obj.del();
                        }
                        layer.alert(obj1.msg);
                    }
                });
                layer.close(index);
            });
        }
    });
    //-------留言管理表格e--------


    //-------文章发布s--------
    form.on('submit(publishSubmit)', function(data){
        $.ajax({
            type: "post"
            , url: "/admin/publish/add"
            , dataType: 'json'
            , data: data.field
            , success: function (obj) {
                layer.msg(obj.msg);
                if (obj.code === 1) {
                    document.getElementById("Publish").reset();
                }
            }
        });

        return false;
    });
    //-------文章发布e--------

    //-------文章管理表格s--------
    var article = table.render({
        elem: '#article-table'
        , height: 'full-180'
        , url: '/admin/article/ajax'
        , page: true
        , cols: [[
            {field: 'articid', title: 'ID'}
            , {field: 'title', title: '文章标题'}
            , {field: 'sort', title: '文章分类'}
            , {field: 'userid', title: '作者'}
            , {field: '', title: '操作', toolbar: '#articleBar'}
        ]]
    });
    table.on('tool(article-table-filter)', function (obj) {
        var data = obj.data;
        if (obj.event == 'del') {
            layer.confirm('是否要删除' + data.title + '这篇文章', function (index) {
                $.ajax({
                    type: 'post'
                    , url: '/admin/article/dell'
                    , dataType: 'json'
                    , data: {articid: data.articid}
                    , success: function (obj1) {
                        if (obj1.code == 1) {
                            obj.del();
                        }
                        layer.alert(obj1.msg);
                    }
                });
                layer.close(index);
            });
        }else if (obj.event == 'edit') {
            layer.open({
                type: 2 //Page层类型
                , area: ['550px', '400px']
                , title: '修改文章'
                , shade: 0.6 //遮罩透明度
                , maxmin: true //允许全屏最小化
                , anim: 1 //0-6的动画形式，-1不开启
                , content: '/admin/article/edit?articid='+data.articid
                , end: function () {
                    article.reload('article-table');
                }
            });
        }else if (obj.event == 'show-comment') {
            layer.open({
                type: 2 //Page层类型
                , area: ['550px', '400px']
                , title: '查看评论'
                , shade: 0.6 //遮罩透明度
                , maxmin: true //允许全屏最小化
                , anim: 1 //0-6的动画形式，-1不开启
                , content: '/admin/article/comment?articid='+data.articid
            });
        }else if (obj.event == 'show-article') {
            layer.msg('查看文章');
        }
    });
    //-------文章管理表格e--------

});